/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.dao.impl;

import com.ivanbiz.dao.DepositDAO;
import com.ivanbiz.dao.JurnalDAO;
import com.ivanbiz.model.Deposit;
import com.ivanbiz.model.GLAccount;
import com.ivanbiz.model.Jurnal;
import com.ivanbiz.model.Kamar;
import com.ivanbiz.model.Kos;
import com.ivanbiz.model.Pembayaran;
import com.ivanbiz.service.HibernateUtil;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author City_Z
 */
public class DepositDAOImpl extends GenericDAOImpl implements DepositDAO {

    @Override
    public String saveDeposit(Deposit deposit, Pembayaran pembayaran) throws Exception {
        String status = "";
        try {
            HibernateUtil.beginTransaction();
            Session session = HibernateUtil.getSession();
            session.save(deposit);
            pembayaran.setTransactionReference("Deposit-" + deposit.getKamar().getKos().getKodeKos() + deposit.getKamar().getKodeKamar());
            pembayaran.setKreditBankAccount(pembayaran.getKreditBankAccount());
            GLAccount glAccountDebet = (GLAccount) getDataByEqual(GLAccount.class, "noGL", String.valueOf(deposit.getKamar().getKos().getKodeKos() + deposit.getKamar().getKodeKamar()).toString(), session);
            pembayaran.setDebitBankAccount(glAccountDebet);
            pembayaran.setDateCreated(new Date());
            pembayaran.setDatePosting(new Date());
            pembayaran.setStatus("0");
            pembayaran.setDeskripsi("Deposit-" + glAccountDebet.getNoGL());
            pembayaran.setTransactionReference("Deposit-" + glAccountDebet.getNoGL());
            session.save(pembayaran);
            JurnalDAO jurnalDAO = new JurnalDAOImpl();
            jurnalDAO.saveJurnal("001002", pembayaran.getJumlah(), "Deposit-" + deposit.getKamar().getKos().getKodeKos() + deposit.getKamar().getKodeKamar(), deposit.getKamar().getKos().getKodeKos() + deposit.getKamar().getKodeKamar(), pembayaran.getKreditBankAccount().getNoGL(), session);
            HibernateUtil.commitTransaction();
            status = "sukses";
        } catch (Exception e) {
            e.printStackTrace();
            status = "error";
            HibernateUtil.rollbackTransaction();
        } finally {
            HibernateUtil.closeSession();
        }
        return status;
    }

    @Override
    public String refundDeposit(String kodeKos, String kodeKamar) throws Exception {
        String status = "";
        try {
            HibernateUtil.beginTransaction();
            Session session = HibernateUtil.getSession();
            System.out.println("mlebu");
            JurnalDAO jurnalDAO = new JurnalDAOImpl();
            Query query = session.createQuery("from com.ivanbiz.model.Jurnal p where p.transactionReference = 'Deposit-" + kodeKos + kodeKamar + "' and status = '2000'");
            List listJurnal = query.list();
            System.out.println(listJurnal.size());
            for (int x = 0; x < listJurnal.size(); x++) {

                Jurnal jurnal = (Jurnal) listJurnal.get(x);
                Jurnal jurnal1 = new Jurnal();
                jurnal1.setAccountingReference(jurnal.getAccountingReference());
                jurnal1.setCurrency(jurnal.getCurrency());
                jurnal1.setDateReference(new Date());
                jurnal1.setGLAccount(jurnal.getGLAccount());
                jurnal1.setStatus("3000");
                jurnal1.setTransactionReference(jurnal.getTransactionReference());
                jurnal1.setCredit(jurnal.getDebit());
                jurnal1.setDebit(jurnal.getCredit());
                session.save(jurnal1);
                System.out.println("ini save" + x);
            }
            if (listJurnal.size() > 0) {
                query = session.createQuery("from com.ivanbiz.model.Kos p where p.kodeKos = '" + kodeKos + "'");
                Kos kos = (Kos) query.uniqueResult();
                query = session.createQuery("from com.ivanbiz.model.Kamar p where p.kodeKamar = '" + kodeKamar + "' and p.kos.id ='" + kos.getId() + "'");
                Kamar kamar = (Kamar) query.uniqueResult();
                query = session.createQuery("from com.ivanbiz.model.Deposit p where p.kamar.id = '" + kamar.getId() + "' and status ='0' ");
                Deposit deposit = (Deposit) query.uniqueResult();
                System.out.println(deposit.getJumlah());
                deposit.setStatus("9");
                session.update(deposit);
            }

            HibernateUtil.commitTransaction();
            status = "sukses";
        } catch (Exception e) {
            e.printStackTrace();
            status = "error";
            HibernateUtil.rollbackTransaction();
        } finally {
            HibernateUtil.closeSession();
        }
        return status;
    }
}
